package com.bjpowernode.boss.common.listener;

import cn.hutool.core.date.DateUtil;
import com.bjpowernode.boss.common.event.UserConfigMailEvent;
import com.bjpowernode.boss.mvc.model.mail.Attachment;
import com.bjpowernode.boss.mvc.model.mail.MailProperty;
import jakarta.annotation.Resource;
import jakarta.mail.MessagingException;
import jakarta.mail.internet.MimeMessage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 定义 监听器
 * 2.创建事件监听器类， 一般叫做XXXListener , 对指定的事件感兴趣。实现ApplicationListener接口
 *  * 	---> 在里面编写 事件发生时执行的方法
 *  * 	--->  使用@Component注解添加到容器中
 */
@Component
@Slf4j  //相当于实现了 Logger log = org.slf4j.LoggerFactory.getLogger(LogExample.class);
public class UserConfigMailListener implements ApplicationListener<UserConfigMailEvent> {

    @Resource
    private JavaMailSender javaMailSender;

    // 当 事件触发执行的方法
    @Override
    @Async
    public void onApplicationEvent(UserConfigMailEvent event) {
        // 在这里进行发送 邮件的功能
        log.info(DateUtil.now() + "--> 给商户发送邮件功能开始-----> ");
        // 获取数据
        MailProperty mailPropertyMsg = event.getMailPropertyMsg();
        // 判断是否存在附件 ---> 根据是否存在附件 , 而确定使用 简单发送  还是 附件发送
        List<Attachment> attachmentList = mailPropertyMsg.getAttachmentList();
        if (attachmentList == null) {
            SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
            simpleMailMessage.setFrom(mailPropertyMsg.getFrom());
            simpleMailMessage.setTo(mailPropertyMsg.getTo());
            simpleMailMessage.setSubject(mailPropertyMsg.getSubject());
            simpleMailMessage.setText(mailPropertyMsg.getText());
            // 发送邮件
            javaMailSender.send(simpleMailMessage);
        } else {
            MimeMessage mimeMessage = javaMailSender.createMimeMessage();
            MimeMessageHelper mimeMessageHelper = null;
            try {
                mimeMessageHelper = new MimeMessageHelper(mimeMessage, true);
                mimeMessageHelper.setFrom(mailPropertyMsg.getFrom());
                mimeMessageHelper.setTo(mailPropertyMsg.getTo());
                for (Attachment attachment : attachmentList) {
                    mimeMessageHelper.addAttachment(attachment.getFileName(), attachment.getFile());
                }
                mimeMessageHelper.setSubject(mailPropertyMsg.getSubject());
                mimeMessageHelper.setText(mailPropertyMsg.getText());
                javaMailSender.send(mimeMessageHelper.getMimeMessage());
            } catch (MessagingException e) {
                e.printStackTrace();
            }
        }
        log.info(DateUtil.now() + "--> 给商户发送邮件功能-----> 执行完毕 ");
    }
}
